package com.itheima.listener;

import com.itheima.domain.mongo.MovementScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(consumerGroup = "recommend-movement-group", topic = "recommend-movement")
public class MovementScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        System.out.println(map + "记录用户行为到mongo中。。。");
        // 获取用户行为数据
        Long userId = Long.valueOf(map.get("userId").toString());
        Long pid = Long.valueOf(map.get("pid").toString());
        Integer type = Integer.parseInt(map.get("type").toString());

        // 封装movementScore实体
        MovementScore movementScore = new MovementScore();
        movementScore.setDate(System.currentTimeMillis());
        movementScore.setUserId(userId);
        movementScore.setPublishId(pid);
        switch (type) {
            case 1: {
                movementScore.setScore(20d);
                break;
            }
            case 2: {
                movementScore.setScore(1d);
                break;
            }
            case 3: {
                movementScore.setScore(5d);
                break;
            }
            case 4: {
                movementScore.setScore(8d);
                break;
            }
            case 5: {
                movementScore.setScore(10d);
                break;
            }
            case 6: {
                movementScore.setScore(-5d);
                break;
            }
            case 7: {
                movementScore.setScore(-8d);
                break;
            }
        }

        // 保存数据到mongo中
        mongoTemplate.save(movementScore);
    }
}
